import request from '../../util/request.js'
import getAccessToken from './getAccessToken.js';
import express from 'express'
var router = express.Router()

export const getTemplateList = (data) => {
  return new Promise(async (resolve, reject) => {
    try {
      const rows = await getAccessToken(data)
      request.get(`https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token=${rows.access_token}`).then((result) => {
        resolve(result)
      });
    } catch (error) {
      reject(error)
    }
  })
}
router.get('/getTemplateList', async (req, res, next) => {
  const payload = {
    appid: process.env.MINIAPPID,
    secret: process.env.MINISECRET
  }
  console.log('payload: ', payload);
  try {
    const rows = await getTemplateList(payload)
    res.json(req.app.locals.success(rows.data))
    
  } catch (error) {
    console.log('error: ', error);
    
    res.json(req.app.locals.error(error))
  }
})
export default router